package com.uyaer.wheel.display.scene.role
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.common.Config;
	import com.uyaer.wheel.common.Platform;
	import com.uyaer.wheel.display.scene.el.ElPool;
	import com.uyaer.wheel.display.scene.el.ElementBase;
	import com.uyaer.wheel.events.EventType;
	import com.uyaer.wheel.res.ResManager;
	import com.uyaer.wheel.tick.TimerManager;
	import com.uyaer.wheel.utils.Utils;
	
	import flash.geom.Point;
	
	import starling.animation.Transitions;
	import starling.core.Starling;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	
	public class Ball extends Sprite
	{
		private var normal:MovieClip;
		private var happy:MovieClip;
		private var pop:MovieClip;
		
		private var _isHappy:Boolean = false;
		private var el:ElementBase;
		
		public function Ball()
		{
			normal = new MovieClip(ResManager.me.getTextures("home_ball_normal"),4);
			addChild(normal);
			Starling.juggler.add(normal);
			happy = new MovieClip(ResManager.me.getTextures("home_ball_happy"),5);
			pop = new MovieClip(ResManager.me.getTextures("home_ball_pop"),4);
			pop.loop = false;
			pop.stop();
			pop.addEventListener(Event.COMPLETE,onPopFrameEndHandler);
			
		}
		
		/**
		 *吐泡泡动画完成 
		 * @param e
		 * 
		 */		
		private function onPopFrameEndHandler(e:Event):void{
			if(Config.platform == Platform.WEB){
				_isHappy = false;
			}
			pop.stop();
			Starling.juggler.remove(pop);
			pop.removeFromParent();	
			
			if(isHappy){
				addChild(happy);
				happy.play();
				Starling.juggler.add(happy);
			}else{
				addChild(normal);
				normal.play();
				Starling.juggler.add(normal);
			}
			
			var p:Point = el.localToGlobal(new Point());
			el.x = p.x;
			el.y = p.y;
			if(el.y < Common.H){
				el.y += Common.H;
			}
			if(stage){
				stage.dispatchEventWith(EventType.HOME_ADD_POP,false,el);
			}else{
				el.recycle();
			}
		}
		
		public function popEl():void{
			TimerManager.addLoop(_popEl,5000);
		}
		
		private function _popEl():void{
			//播放mc
			happy.removeFromParent();
			happy.stop();
			Starling.juggler.remove(happy);
			normal.removeFromParent();
			normal.stop();
			Starling.juggler.remove(normal);
			
			pop.currentFrame = 0;
			pop.play();
			Starling.juggler.add(pop);
			addChild(pop);
			//生成pop
			if(!isHappy && Utils.probability(0.9)){
				el = ElPool.getCloud();
			}else{
				el = Utils.probability(0.5)?ElPool.getLove():ElPool.getPop();
			}
			el.scaleX = el.scaleY = 0.01;
			el.alpha =0;
			el.x = 150;
			el.y = 180;
			addChild(el);
			var sr:Number = Utils.rangInt(80,100) / 100.0 * Common.S;
			Starling.juggler.tween(el,1,{"scaleX":sr,"scaleY":sr,"alpha":1,"transition":Transitions.EASE_IN_BACK});
		}
		
		public function stopEl():void{
			TimerManager.removeLoop(_popEl);
		}

		public function get isHappy():Boolean{
			return _isHappy;
		}

		public function set isHappy(value:Boolean):void{
			_isHappy = value;
			if(value){
				if(normal.parent){
					normal.removeFromParent();
					normal.stop();
					Starling.juggler.remove(normal);
					
					addChild(happy);
					happy.play();
					Starling.juggler.add(happy);
				}
			}
		}

	}
}